<%@ page language="java" pageEncoding="utf-8"%>
<%@ page import="wpmp.security.client.utils.ButtonConfigUtils"%>
<%@ include file="/commons/taglibs.jsp"%>
<html>
<head>
<title>用户管理</title>
<%@ include file="/commons/meta.jsp" %>
<%@ include file="/commons/header.jsp" %>
<%@ include file="/extjs/jsps/importExt.jsp" %>
<%@ include file="/extjs/jsps/importEasyExt.jsp" %>
<script language="javascript" type="text/javascript">
/**
 * 重置密码
 */
function resetPassword(id) {
	Ext.Msg.confirm("","确认重置密码？",function(e) {
		if(e=='no') {
			return;
		}
		Common.ajaxRequest('${ctx}/user!resetPasswordJSON.action',{
			id: id
		});
	});
}
/**
 * 授权用户角色
 * @param id 用户ID
 */
function resetRole(id) {
	var paraUrl="&id="+id;
	var url="${ctx}/user!listRole.action?winId=winUserListRole&U="+(new Date().getTime())+paraUrl;
	windowOpen(url,'winUserListRole','授权角色',355,315,'','',{
		onClose: function(scope) {
			var obj=scope.returnValue;
			if(obj) {
			}
		}
	});
}

var store = new Ext.data.JsonStore({
	autoDestroy: true,
	autoLoad: true,
	remoteSort: true,
	url: '${ctx}/user!listJSON.action',
   	root: 'records',
   	paramNames: {
   	    start: 'page.start',
   	    limit: 'page.limit',
   	    sort: 'page.sort',
   	    dir: 'page.dir'
   	},
   	baseParams: {},
	fields: [
        {name: 'id',mapping: 'id',type: 'string'},
        {name: 'loginName',mapping: 'loginName',type: 'string'},
        {name: 'userName',mapping: 'userName',type: 'string'},
        {name: 'mobile',mapping: 'mobile',type: 'string'},
        {name: 'tel',mapping: 'tel',type: 'string'},
        {name: 'orgName',mapping: 'orgName',type: 'string'},
        {name: 'updateUser',mapping: 'updateUser',type: 'string'},
        {name: 'updateUserName',mapping: 'updateUserName',type: 'string'},
        {name: 'updateTime',mapping: 'updateTime',type: 'string'},
        {name: 'isAdmin',mapping: 'isAdmin',type: 'boolean'}
	]
});

var sm = new Ext.grid.CheckboxSelectionModel({
	singleSelect: false
});

var cm = new Ext.grid.ColumnModel({
	defaults: {
		align: 'center',
		sortable: true
	},
	columns: [
		new Ext.grid.RowNumberer(), 
		sm,
		{header: '登录名',dataIndex: 'loginName',width: 15},
		{header: '姓名',dataIndex: 'userName',width: 10},
		{header: '组织/单位',dataIndex: 'orgName',width: 15},
		{header: '联系电话',dataIndex: 'mobile',width: 10,
			renderer: function(value,metaData,record,rowIndex,colIndex,store) {
				// 优先取移动电话
				return Ext.isEmpty(value)?record.get("tel"):value;
			}
		},
		{header: '是否管理员',dataIndex: 'isAdmin',width: 8,
			renderer: function(value,metaData,record,rowIndex,colIndex,store) {
				return value==true?'是':'否';
			}
		},
		{header: '更新者',dataIndex: 'updateUser',width: 12,
			renderer: function(value,metaData,record,rowIndex,colIndex,store) {
				return record.get("updateUserName");
			}
		},
		{header: '更新时间',dataIndex: 'updateTime',width: 15},
		{header: '操作',dataIndex: 'id',width: 15,sortable: false,
			renderer: function(value,metaData,record,rowIndex,colIndex,store) {
				return '<a href="javascript:void(0)" style="color:blue" onclick="resetRole('+value+');">授权角色</a>'+
						'&nbsp;|&nbsp;<a href="javascript:void(0)" style="color:blue" onclick="resetPassword('+value+');">重置密码</a>';
			}
		}
	]
});

var pageSizePlugin = new EasyExt.widgets.PageSizePlugin();

var toolbarItem = [''
	<%if(!ButtonConfigUtils.hidden(request, response, "user.list.add")){%>
	,{
		text: '新建',
		iconCls: 'addBtn',
		handler: function() {
			var url="${ctx}/user!input.action?winId=winInsertUser&U="+(new Date().getTime());
			windowOpen(url,'winInsertUser','新建用户',315,785,'','',{
				onClose: function(scope) {
					var obj=scope.returnValue;
					if(obj) {
						store.reload();
					}
				}
			});
		}
	},'-'
	<%}%>
	<%if(!ButtonConfigUtils.hidden(request, response, "user.list.edit")){%>
	,{
		text: '编辑',
		iconCls: "editBtn",
		handler: function() {
			if (sm.getCount()!=1) {
				Common.showErrorMsg('必须且仅能选择一条记录！');
				return;
			}
			var paraUrl="&id="+sm.getSelections()[0].get("id");
			var url="${ctx}/user!input.action?winId=winUpdateUser&U="+(new Date().getTime())+paraUrl;
			windowOpen(url,'winUpdateUser','更新用户',315,785,'','',{
				onClose: function(scope) {
					var obj=scope.returnValue;
					if(obj) {
						store.reload();
					}
				}
			});
		}
	},'-'
	<%}%>
	<%if(!ButtonConfigUtils.hidden(request, response, "user.list.delete")){%>
	,{
		text: '删除',
		iconCls: "deleteBtn",
		handler: function() {
			if (sm.getCount()==0) {
				Common.showErrorMsg('请至少选择一条记录！');
				return;
			}
			Ext.Msg.confirm('','用户删除后将无法恢复，确认删除？',function(e) {
				if(e=='no') {
					return;
				}
				Common.ajaxRequest('${ctx}/user!deleteJSON.action',{
					ids: Common.getSelectedProperties(sm,'id')
				},function(success) {
					if(success) {
						store.load();
					}
				});
			});
		}
	},'-'
	<%}%>
	<%if(!ButtonConfigUtils.hidden(request, response, "user.list.authRole")){%>
	,{
		text: '授权角色',
		iconCls: "ticketBtn",
		handler: function() {
			if(sm.getCount()==0) {
				Common.showErrorMsg('请至少选择一条记录！');
				return;
			}
			var url="${ctx}/role!select.action?winId=winSelectRole&U="+(new Date().getTime());
			windowOpen(url,'winSelectRole','选择角色',355,315,'','',{
				onClose: function(scope) {
					var obj=scope.returnValue;
					if(obj) {
						var roleIds=new Array();
						for(var i=0;i<obj.length;i++) {
							roleIds.push(obj[i].id);
						}
						Common.ajaxRequest('${ctx}/user!addRoleJSON.action',{
							ids: Common.getSelectedProperties(sm,'id'),
							roleIds: roleIds
						},Ext.emptyFn,false);
					}
				}
			});
		}
	},'-'
	<%}%>
	,{
		xtype : 'textfield',
		emptyText: '输入关键字按回车',
		cls : 'SearchFieldCls',
		enableKeyEvents:true,
		listeners: {
			keyup: function (thiz, eventObj) {
				store.setBaseParam("kw", thiz.getValue());
				if (eventObj.getKey() == eventObj.ENTER) {
					var limit = pageSizePlugin.getValue();
					if (!limit) {
						limit = 25;
					}
					store.setBaseParam("start", 0);
					store.setBaseParam("limit", limit);
					store.load();
				}
			}
		}
	},'-'
];

var gridPanel = new Ext.grid.EditorGridPanel({
	style: 'text-align:left;',
	border: true,
	stripeRows: true,// 斑马线效果
	loadMask: {
		msg: '加载数据中, 请稍侯... ...'
	},
	trackMouseOver: true,
	store: store,
	cm: cm,
	sm: sm,
	clicksToEdit: 1,// 单击编辑
	autoExpandColumn: 'userName',
	viewConfig: {
		scrollOffset: 20,// 滑动条宽度，默认是20
		forceFit: true
	},
	tbar: Common.pageingToolBar(pageSizePlugin,store,toolbarItem)
});

Ext.onReady(function() {
	var viewport = new Ext.Viewport({
		layout: 'fit',
		items: [gridPanel]
	});
});
</script>
</head>

<body>
</body>
</html>